package com.ncloud.resman.mapper;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.resman.entity.DeviceIp;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by shiqiang on 2017/8/14.
 */
@Mapper
public interface DeviceIpMapper {
    @Insert("insert into res_device_ip(id,device_id, ip, use_tag)"
            + " values(#{id},#{deviceId},#{ip},#{useTag})")
    int insertDeviceIp(DeviceIp deviceIp);
    @Update("update res_device_ip set device_id = #{deviceId},ip=#{ip},use_tag =#{useTag} where id =#{id}")
    int updateDeviceIp(DeviceIp deviceIp);
    /*删除*/
    @Delete(value = "delete from res_device_ip WHERE device_id = #{0}")
    public int deleteDeviceIp(Long deviceId);
    
    @Select("SELECT CONCAT(id,'') as id, CONCAT(device_id,'') as device_id, ip, use_tag "
			+ "FROM res_device_ip where device_id = #{deviceId}")
	@Results({
		@Result(column="id",property="id"),@Result(column="device_id",property="deviceId"),
		@Result(column="ip",property="ip"),@Result(column="use_tag",property="useTag")})
	List<JSONObject> getDeviceIpById(Long deviceId);//根据设备id查询设备服务器信息
    
    @Select("select a.ip from res_device_ip a , res_device_base b where a.device_id=b.device_id "
    		+ "and if({0} is null,1=1,b.sn_num={0})")
	@Results({
		@Result(column="ip",property="ip")})
    
	List<DeviceIp> getDeviceIpBySnNum(String snNum);//根据设备snNum查询设备服务器信息
}
